package com.tan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tan.domain.Blog;
import com.tan.domain.Type;
import com.tan.domain.response.Result;
import com.tan.domain.response.StatusCode;
import com.tan.domain.vo.TypeVo;
import com.tan.service.BlogService;
import com.tan.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * @description:
 * @author: longshifu
 * @time: 2022/7/20 12:08
 */
@RestController
@Slf4j
public class TypeShowController {
    @Autowired
    private TypeService typeService;
    @Autowired
    BlogService blogService;

    @GetMapping("/getTypeList")
    public Result getTypeList() {
//        Page<Type> page = new Page<>(0, 6);
//        page = typeService.page(page);
//        List<Type>typeList = page.getRecords();
        List<Type> typeList = typeService.list();
        List<TypeVo> typeVos = new LinkedList<>();

        for (Type type : typeList) {
            TypeVo v = new TypeVo();
            BeanUtils.copyProperties(type, v);
            typeVos.add(v);
        }
        for (TypeVo type : typeVos) {
            QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type_id", type.getId());
            int count = blogService.count(queryWrapper);
            type.setBlogCount(count);
        }
        return new Result(true, StatusCode.OK, "获取博客分类成功",typeVos);
    }

}
